package com.hrma.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hrma.dao.SalaryLevelMapper;
import com.hrma.entity.SalaryLevel;
import com.hrma.entity.SalaryLevelDTO;
import com.hrma.entity.SalaryLevelVO;
import com.hrma.entity.SelectResult;
import com.hrma.services.SalaryLevelService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SalaryLevelImpl implements SalaryLevelService {
    private final SalaryLevelMapper salaryLevelMapper;

    public SalaryLevelImpl(SalaryLevelMapper salaryLevelMapper) {
        this.salaryLevelMapper = salaryLevelMapper;
    }

    @Override
    public SalaryLevel add(SalaryLevel data) {
        if (!salaryLevelMapper.exist(data.getId())) {
            throw new RuntimeException("id字段不存在！");
        }
        salaryLevelMapper.add(data);
        return data;
    }

    @Override
    public void delete(int id) {
        if (!salaryLevelMapper.exist(id)) {
            throw new RuntimeException("id字段不存在！");
        }
        salaryLevelMapper.delete(id);
    }

    @Override
    public void update(List<SalaryLevelVO> data) {
        SalaryLevel salaryLevel = new SalaryLevelDTO();
        for (SalaryLevelVO i : data) {
            salaryLevel.setLevel(i.getLevel());

            salaryLevel.setGear(1);
            salaryLevel.setMoney(i.getGear1());
            salaryLevelMapper.update(salaryLevel);
            salaryLevel.setGear(2);
            salaryLevel.setMoney(i.getGear2());
            salaryLevelMapper.update(salaryLevel);
            salaryLevel.setGear(3);
            salaryLevel.setMoney(i.getGear3());
            salaryLevelMapper.update(salaryLevel);
            salaryLevel.setGear(4);
            salaryLevel.setMoney(i.getGear4());
            salaryLevelMapper.update(salaryLevel);
            salaryLevel.setGear(5);
            salaryLevel.setMoney(i.getGear5());
            salaryLevelMapper.update(salaryLevel);
            salaryLevel.setGear(6);
            salaryLevel.setMoney(i.getGear6());
            salaryLevelMapper.update(salaryLevel);
            salaryLevel.setGear(7);
            salaryLevel.setMoney(i.getGear7());
            salaryLevelMapper.update(salaryLevel);
            salaryLevel.setGear(8);
            salaryLevel.setMoney(i.getGear8());
            salaryLevelMapper.update(salaryLevel);
            salaryLevel.setGear(9);
            salaryLevel.setMoney(i.getGear9());
            salaryLevelMapper.update(salaryLevel);
            salaryLevel.setGear(10);
            salaryLevel.setMoney(i.getGear10());
            salaryLevelMapper.update(salaryLevel);
        }
    }

    @Override
    public List<SelectResult> selectSalary() {
        return salaryLevelMapper.selectSalary();
    }

    @Override
    public Page<SalaryLevel> getList(SalaryLevelDTO data) {
        PageHelper.startPage(data.getPageNum(),data.getPageSize());
        return salaryLevelMapper.getList(data);
    }

    @Override
    public List<SalaryLevelVO> getSalaryLevel() {
        return salaryLevelMapper.getSalaryLevel();
    }
}
